﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using Breeze.WebApi;
using onYOURway.Models;
using System.Data.Spatial;

namespace onYOURway.Controllers {
  
  [BreezeController]
  public class LocateController : ApiController {

    readonly EFContextProvider<onYOURwayEntities> db = new EFContextProvider<onYOURwayEntities>();

    [HttpGet]
    public string Metadata() {
      return db.Metadata();
    }

    [HttpGet]   // api/locate/Regions
    public IQueryable<Region> Regions() {
      return db.Context.Regions
        .Include("Views")
        .Include("Aliases")
        ;
    }

    [HttpGet]   // api/locate/Locations
    public IQueryable<dynamic> Locations(string CloseTo = null) {
      var _lang = "de";
      return db.Context.Locations
        .Include("Tags")
        .Include("Tags.Names")
        //.Include("Aliases")
        .Include("IsOpens")
        .Select(l => new {
          Id = l.Id, 
          Name = l.Name,
          //Street = ((l.Street ?? "") + " " + (l.HouseNumber ?? "")).Trim(),
          Street = l.Street ?? "",
          HouseNumber = l.HouseNumber ?? "",
          Zip = l.Zip ?? "",
          City = l.City ?? "",
          Position = l.Position,
          Group = l.Tags.Where(t => t.Type == "Class").FirstOrDefault().Names.Where(n => n.Lang == _lang).FirstOrDefault().Name,
          Kind = l.Tags.Where(t => t.Type == "Branche").FirstOrDefault().Names.Where(n => n.Lang == _lang).FirstOrDefault().Name,
          IsOpens = l.IsOpens,
          Tags = l.Tags.Select(t => new {
            Id = t.Id,
            Names = t.Names.Where(n => n.Lang == _lang).Select(n => n.Name)
          }),
          Distance = string.IsNullOrEmpty( CloseTo ) ? null : l.Position.Distance(DbGeography.FromText(CloseTo, 4326))
        })
        .OrderBy(l => l.Distance)
        ;
    }

    [HttpGet]
    public DbGeographyWellKnownValue Buffer(string wkt, int meters) {
      return DbGeography.FromText(wkt, 4326).Buffer(meters).WellKnownValue;
    }

    [HttpGet]
    public IQueryable<AutoComplete_Result> AutoComplete() {
      long RegionId = 1;
      return (IQueryable<AutoComplete_Result>)db.Context.AutoComplete(RegionId).ToList();
    }

    [HttpGet]
    public Dictionary<string, object> TransportFeatures(int RegionId) {
      var boundary = db.Context.Regions.Where(r => r.Id == RegionId).First().Boundary;
      var result = new Dictionary<string, object>();
      result.Add("Lines",
        db.Context.TransportLines.ToArray() //.Where(l => l.Way.Intersects(boundary)).ToArray()
        );
      result.Add("Stops",
        db.Context.TransportStops.ToArray() //.Where(s => s.Position.Intersects(boundary)).ToArray()
      );
      return result;
    }

    [HttpGet]
    public IQueryable<BikeWay> BikeFeatures(int RegionId) {
      //var boundary = db.Context.Regions.Where(r => r.Id == RegionId).First().Boundary;
      //var result = new Dictionary<string, object>();
      //result.Add("BikeWays",
      //  db.Context.BikeWays.ToArray() //.Where(l => l.Way.Intersects(boundary)).ToArray()
      //  );
      //return result.ToArray();
      return db.Context.BikeWays;
    }

  } //class

} //ns
